$(function() {
	var json;
	var json_vocc;
	var json_port;
	var json_log;
	var json_country;
	var json_vessel;
	function FillSpace(str,len){
		u=ulen(str);
		if(u<len.toString()){
			for(var i=0;i<len-u;i++){
				str+="&nbsp;";
			}
		}
		return str;
	}
	//通过船公司id获得船公司的名字
	function Get_VoccName(vocc_id){
		var vocc_name="";
		$.each(json_vocc,function(i,item){
			if(vocc_id==item.vocc_id){
				vocc_name=item.vocc_name;
				return false;
			}
		});
		return vocc_name;
	}
	//通过港口id获得港口的名字
	function Get_PortName(port_id){
		var port_name="";
		$.each(json_port,function(i,item){
			if(port_id==item[0]){
				port_name=item[1];
				return false;
			}
		});
		return port_name;
	}
	//通过国家id获得国家的名字
	function Get_CountryName(country_id){
		var country_name="";
		$.each(json_country,function(i,item){
			if(country_id==item[0]){
				country_name=item[1];
				return false;
			}
		});
		return country_name;
	}
	function ulen(str)
	{
		regex = new RegExp("^[\u4e00-\u9fa5|\uFF00-\uFFFF]+$");
		var nLength = str.length;
		for(var i=0; i<str.length; i++)
		{
		if (regex.test(str.substring(i,i+1)))  
			nLength++;
		}
		return nLength;

	}	
	//初始化国家列表
	function Init_Country(){
		var country_option="";
		$.each(json_country,function(i,item){
			country_option+='<option value="'+item[0]+'">'+item[1]+'</option>';
		});
		$("#country").append(country_option);
	}
	
	//初始化船公司列表
	function Init_Vocc(){
		var vocc_option="";
		$.each(json_vocc,function(i,item){
			vocc_option+='<option value="'+item.vocc_id+'">'+FillSpace(item.vocc_name,15)+item.vocc_chinese+'</option>';
		});
		$("#vocc").append(vocc_option);
	}
	//通过船公司id设置船公司选择列表为选中状态
	function Set_Vocc(vocc_id){
		$.each(json_vocc,function(i,item){
			if(item.vocc_id==vocc_id){
				$("#vocc").attr('value',vocc_id);
				return false;
			}
		});
	}
	//初始化港口列表，港口列表可用于多个地方
	function Init_Port(){
		var port_option="";
		var country_id="";
		$.each(json_port,function(i,item){
			if(item[2]!=country_id){
				if(country_id!=""){
					port_option+='</optgroup">';
				}
				country_id=item[2];
				port_option+='<optgroup label="'+Get_CountryName(country_id)+'">';
			}
			port_option+='<option value="'+item[0]+'">'+item[1]+'</option>';
		});
		$("#registry").append(port_option);
		$("#start,#end").append(port_option);
	}
	//通过港口id设置登记港选择列表为选中状态
	function Set_Registry(port_id){
		$("#registry").attr('value',port_id);
	}
	//通过国家id设置国籍选择列表为选中状态
	function Set_Country(country_id){
		$("#country").attr('value',country_id);
	}
	//通过港口id设置起始港选择列表为选中状态
	function Set_Start(port_id){
		$("#start").attr('value',port_id);
	}
	//通过港口id设置目的港选择列表为选中状态
	function Set_End(port_id){
		$("#end").attr('value',port_id);
	}
	//初始化今天的记录
	function Init_Log(){
		Update_Log(json_log);
	}
	//更新当前操作记录
	function Update_Log(log){
		var vessel_row=""
		$.each(log,function(i,item){
			if(item.vessel_id==vessel_id||submit!=""){
				vessel_row+="<tr data-vessel='"+item.vessel_id+"' class='note'>";
			}else{
				vessel_row+="<tr data-vessel='"+item.vessel_id+"'>";
			}
			vessel_row+="<th class='sub' scope='row'>"+item.vessel_name+"</th>";
			vessel_row+="<td>"+Get_VoccName(item.vessel_vocc)+"</td>";
			vessel_row+="<td>"+Get_PortName(item.vessel_registry)+"</td>";
			vessel_row+="<td>"+Get_CountryName(item.vessel_country)+"</td>";
			vessel_row+="<td>"+Get_PortName(item.vessel_start)+"</td>";
			vessel_row+="<td>"+Get_PortName(item.vessel_end)+"</td>";
			vessel_row+="<td>"+item.vessel_chinese+"</td>";
			vessel_row+="<td>"+item.vessel_english+"</td>";
			vessel_row+="<td>"+item.vessel_short+"</td>";
			vessel_row+="<td>"+item.vessel_update+"</td>";
			vessel_row+="<td>"+item.vessel_memo+"</td>";
			vessel_row+="</tr>";
		});
		$("#tbl_vessel>tbody>tr:first").after(vessel_row).next().next().removeClass("note");
	}
	//通过船公司id获得船公司的信息
	function Get_VoccInfo(vocc_id){
		$.each(json_vocc,function(i,item){
			if(vocc_id==item.vocc_id){
				vocc_name=item.vocc_name;
				$("#vocc_id").val(item.vocc_id);
				$("#vocc_name").html(item.vocc_name);
				$("#vocc_chinese").html(item.vocc_chinese);
				$("#vocc_english").html(item.vocc_english);
				$("#vocc_short").html(item.vocc_short);
				$("#vocc_memo").html(item.vocc_memo);
				return false;
			}
		});
	}
	
	//显示当前船舶信息
	function View_Vessel(vessel_id,aCurrent){
		$.each(aCurrent,function(i,item){
			if(item.vessel_id==vessel_id){
				Set_Vocc(item.vessel_vocc);
				Set_Country(item.vessel_country);
				Set_Registry(item.vessel_registry);
				Set_Start(item.vessel_start);
				Set_End(item.vessel_end);
				$("#vessel").val(item.vessel_name);
				$("#chinese").val(item.vessel_chinese);
				$("#english").val(item.vessel_english);
				$("#short").val(item.vessel_short);
				$("#memo").val(item.vessel_memo);
				Get_VoccInfo(item.vessel_vocc);
				return false;
			}
		});
	}
	
	function Insert_Vessel(aCurrent){
		json_log.unshift(aCurrent[0]);
	}
	function Delete_Vessel(aCurrent){
		for(var i=0;i<json_log.length;i++){
			if(json_log[i].vessel_id==aCurrent[0].vessel_id){
				json_log.splice(i,1);
				return false;
			}	
		}
	}
	function Update_Vessel(aCurrent){
		Delete_Vessel(aCurrent);
		Insert_Vessel(aCurrent);
	}
	//通过ajax获得json数据
	function Get_Vessel(submit,vessel_id){
		$.getJSON("vessel.asp?vessel_id="+vessel_id+"&submit="+submit+"&"+$("#form1").serialize(),function(data){
			if(data.bStatus){
				json=data;
				if(submit==""){
					json_vocc=data.aVocc;
					json_port=data.aPort;
					json_log=data.aLog;
					json_country=data.aCountry;
					Init_Vocc();
					Init_Country();
					Init_Port();
					if(clkid==""){
						Init_Log();
					}
				}else{
					if(submit=="保存"){
						Update_Vessel(data.aData);
						$("#tbl_vessel>tbody>tr[data-vessel='"+vessel_id+"']").remove();
					}else if(submit=="新增并复制"){
						Insert_Vessel(data.aData);
						$("#vessel").focus(function(){
							$(this).val("");
						});
						$("#vessel").focus();
					}
					Update_Log(data.aData);
				}
				if(data.aData){
					
					View_Vessel(vessel_id,data.aData);
				}
			}else{
				//alert(data.sInfo);
				//$( "#dialog:ui-dialog" ).dialog( "destroy" );
				$( "#dialog-modal" ).html( '<p>'+data.sInfo+'</p>' ).dialog({
					height: 140,
					modal: true,
					//position: 'center',
					buttons: {
						"确定": function() {
							$( this ).dialog( "close" );
							$("#vessel").focus();
						}
					}

				});
			}
		});
	}
	
	//$(".datepicker").datepicker({ minDate: 0, maxDate: "+1w" });
	$("#form1").validationEngine();
	var submit="";
	var url = location.search;
	var vessel_id = $.query.get('vessel_id');
	var clkid="";
	if(vessel_id!=""){
		$("#submit").val("保存");
	}else{
		$("#submit").val("新增并复制");
	}
	//alert(vessel_id);
	Get_Vessel(submit,vessel_id);
	//验证提交的数据
	$("#submit").click(function(){
		submit=$(this).val();
		if(clkid==""){
			Get_Vessel(submit,vessel_id);
		}else{
			Get_Vessel(submit,clkid);
		}
	});
	
	//通过选择船公司显示船公司信息
	$("#vocc").change(function(){
		vocc_id=$(this).val();
		Get_VoccInfo(vocc_id);
	});
	$("#tbl_vessel>tbody>tr:gt(0)").live('dblclick',function(){
		$("#submit").val("保存");
		$("#vessel").unbind('focus');
		clkid=$(this).attr("data-vessel");
		View_Vessel(clkid,json_log);
		$("#tbl_vessel>tbody>tr:gt(0)").removeClass("note");
		$(this).addClass("note");
	});
	
	
});